﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using SAPbouiCOM;

namespace BoAddonBiz_FreightForwarder.Sea
{
    /// <summary>
    /// 集装箱号
    /// </summary>
    /// <author>Webb.Wen</author>
    /// <date>2012-04-19 16:36:11</date>
    public class B1Form_Container : SAPBoAddon.B1Form
    {
        public B1Form_Container()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_Container_frm_ItemEvent);
        }

        void B1Form_Container_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {
                if (pVal.ItemUID == "1" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    Form parentForm = Utils.GetForm(this.SBOApp, this.CurrentForm.DataSources.UserDataSources.Item("ParentUID").Value.Trim());
                    if (parentForm != null)
                    {
                        string OwnerCode = ValidateOwnerCode();
                        string RegistrationCode = ValidateRegistrationCode();
                        if (String.IsNullOrEmpty(OwnerCode))
                        {
                            this.CurrentForm.ActiveItem = "4";
                            BubbleEvent = false;
                        }
                        else if (String.IsNullOrEmpty(RegistrationCode))
                        {
                            this.CurrentForm.ActiveItem = "5";
                            BubbleEvent = false;
                        }
                        else
                        {
                            Matrix mtx = parentForm.Items.Item(this.CurrentForm.DataSources.UserDataSources.Item("Matrix").Value.Trim()).Specific as Matrix;
                            mtx.SetCellWithoutValidation(Convert.ToInt32(this.CurrentForm.DataSources.UserDataSources.Item("Row").Value.Trim()), this.CurrentForm.DataSources.UserDataSources.Item("Col").Value.Trim(), OwnerCode.ToUpper() + RegistrationCode + this.CurrentForm.Items.Item("6").Specific.Value.ToString());
                        }
                    }
                }
            }
            else
            {
                if (pVal.EventType == SAPbouiCOM.BoEventTypes.et_LOST_FOCUS)
                {
                    if (pVal.ItemUID == "4")//箱主代码（Owner Code）四位英文,大写
                    {
                        string OwnerCode = ValidateOwnerCode();
                        if (!String.IsNullOrEmpty(OwnerCode))
                        {
                            //this.CurrentForm.Items.Item("4").Specific.Value = this.CurrentForm.Items.Item("4").Specific.Value.ToString().ToUpper();
                            this.CurrentForm.Items.Item("6").Specific.Value = Utils.GetCheckDigit(this.CurrentForm.Items.Item("4").Specific.Value.ToString().Trim(), this.CurrentForm.Items.Item("5").Specific.Value.ToString().Trim());
                        }
                    }
                    if (pVal.ItemUID == "5")//箱体注册码（Registration Code）六位数字
                    {
                        string RegistrationCode = ValidateRegistrationCode();
                        if (!String.IsNullOrEmpty(RegistrationCode))
                        {
                            this.CurrentForm.Items.Item("6").Specific.Value = Utils.GetCheckDigit(this.CurrentForm.Items.Item("4").Specific.Value.ToString().Trim(), this.CurrentForm.Items.Item("5").Specific.Value.ToString().Trim());
                        }
                    }
                }
            }
        }

        protected override bool BeforeSaveCheckFormData()
        {
            return false;
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {

        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_SE_Container;
            FormSetting.FormFileName = "AVA_SE_ContainerForm.srf";
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            this.CurrentForm.Mode = BoFormMode.fm_ADD_MODE;
        }

        private string ValidateOwnerCode()
        {
            string OwnerCode = this.CurrentForm.Items.Item("4").Specific.Value.ToString().Trim();
            if (Regex.IsMatch(OwnerCode, @"^[a-zA-Z]{4}$"))
            {
                return OwnerCode;
            }
            else
            {
                SBOApp.StatusBar.SetText("箱主代码（Owner Code）验证失败，请输入四位英文", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
                return null;
            }
        }

        private string ValidateRegistrationCode()
        {
            string RegistrationCode = this.CurrentForm.Items.Item("5").Specific.Value.ToString().Trim();
            if (Regex.IsMatch(RegistrationCode, @"^[\d]{6}$"))
            {
                return RegistrationCode;
            }
            else
            {
                SBOApp.StatusBar.SetText("箱体注册码（Registration Code）验证失败，请输入六位数字", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
                return null;
            }
        }
    }
}
